Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GaussianRandomFields to calculate optimal proposal covariance #216

Merged

Conversation

matt-graham
Copy link
Member

Fixes #213 and fixes #214.

Updates the covariance method in src/OptimalFilter.jl to use the apply method in the GaussianRandomFields package rather than hardcoding a particular covariance function. The expected interface of a model is also slightly updated to have get_model_noise_params return a structure defining the parameters of the covariance function. These changes both ensure the covariance function definition used in sampling the state noise for the model and assumed in the optimal proposal distribution match (which may not currently be the case as described in #213) and means the optimal proposal distribution directly generalises to any isotropic covariance function defined in the GaussianRandomFields package.

I also updated some of the comments in src/OptimalFilter.jl to clarify the match to the equations / notation in Dietrich and Newsam (1996), including specifically resolving the TODO described in #214.

src/OptimalFilter.jl Outdated Show resolved Hide resolved
@matt-graham
Copy link
Member Author

Turns out I had not being using my local development version of ParticleDA.jl when running the test locally 🤦 which unsurprisingly meant all the tests were passing. The failing unit tests I think just need me to update these lines

noise_params = (sigma = model_params.sigma[1], lambda = model_params.lambda[1], nu = model_params.nu[1])

noise_params = (sigma = model_params.sigma[1], lambda = model_params.lambda[1], nu = model_params.nu[1])

to reflect that noise_params is now expected to be a GaussianRandomFields covariance structure.

The optimal filter integration test also fails due to mismatch from the reference values. If there was a mismatch between the covariance being assumed in the state noise generation and optimal proposal distribution implementation that would possibly explain this. I'll check whether the values we are getting from the covariance method have changed, and if so regenerate the reference data.

@matt-graham
Copy link
Member Author

I've updated the tests to fix the problems described above, plus adjusting some manual covariance function definitions in optimal_filter_validation.jl and runtests.jl to be consistent with the Matern covariance function being used (specifically using the simplified form of the Matern covariance for ν=0.5 as the test case).

I also noticed what I think is a bug in GaussianRandomFields which means the σ parameter is being ignored when evaluating covariance matrices using apply. I have raised an issue about this: PieterjanRobbe/GaussianRandomFields.jl/issues/45. For now I have manually multiplied the output of apply by σ^2 to give the correctly scaled covariances. If this is confirmed as a bug and fixed upstream in GaussianRandomFields we will need to remove this manual scaling.

test/optimal_filter_validation.jl Outdated Show resolved Hide resolved
@@ -11,4 +11,4 @@ model:
obs_noise_std: 0.01
sigma: 1.0
lambda: 1.0
nu: 2.5
nu: 0.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for changing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I was referring to with

to be consistent with the Matern covariance function being used (specifically using the simplified form of the Matern covariance for ν=0.5 as the test case)

As the Matern covariance function is particularly simple for nu=0.5 I changed to using this value so that in the optimal_filter_validation.jl definition of R

function R(x::Float64, y::Float64, th)
Cov = (th.s^2)*exp(-norm([x, y])/th.l)
return(Cov)
end

we can just use a simple exponential expression rather than having to use SpecialFunctions to load the gamma and besselk functions. It might still be better to do this though I guess from a robustness standpoint so that the test is valid for any value of nu. Or at least I should perhaps put a comment in the YAML file to make clear this value shouldn't be changed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just use a simple exponential expression rather than having to use SpecialFunctions to load the gamma and besselk functions.

If the worry is to load another package during the tests, SpecialFunctions is already pulled in by GaussianRandomFields, so it isn't much more work.

It might still be better to do this though I guess from a robustness standpoint so that the test is valid for any value of nu.

I think that'd be better, yes.

Or at least I should perhaps put a comment in the YAML file to make clear this value shouldn't be changed?

At very least this, yes, please.

src/ParticleDA.jl Outdated Show resolved Hide resolved
matt-graham and others added 2 commits January 28, 2022 09:25
Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>
Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>
Copy link
Member

@giordano giordano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, after deciding what value of nu to use in the tests

Cov = (th.s^2)*exp(-(abs(x)+abs(y))/(2.0*th.l))
Cov = (th.s^2)*exp(-hypot(x, y) / th.l)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two expressions don't look equal, was this an intentional correction?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See discussion above #216 (comment) 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I didn't notice the resolved discussion

Comment on lines -114 to +122
# Covariance between observations R_22, from equationd 3-4 in in Dietrich & Newsam 96
# TODO: Ask Alex why we add sigma^2 on the diagonal
function covariance_stations!(cov::AbstractMatrix{T}, grid::Grid, stations::NamedTuple, noise_params::NamedTuple, std::T) where T
# Covariance between observations R̅₂₂ = R₂₂ + Σ, where R₂₂ is as defined in equations 3 and 4 in
# Dietrich & Newsam 96 and Σ is the observation noise covariance (see note at end of
# Section 2 in Dietrich & Newsam 1996)
function covariance_stations!(cov::AbstractMatrix{T}, grid::Grid, stations::NamedTuple, covariance_structure::IsotropicCovarianceStructure{T}, std::T) where T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching and fixing the TODO 😄

Project.toml Outdated Show resolved Hide resolved
@matt-graham
Copy link
Member Author

I've now put the full definition of the Matern covariance function in optimal_filter_validation.jl so that the corresponding test should work for any nu value used in the YAML file and changed back the nu value used in the YAML file to the previous value of 2.5.

@giordano
Copy link
Member

Looks perfect! Thanks a lot again!

@matt-graham matt-graham merged commit fbb3628 into Team-RADDISH:master Jan 31, 2022
@matt-graham matt-graham deleted the mmg/optimal-proposal-grf-covariance branch January 31, 2022 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants